package Service.servicelmpl;

import Dao.DaoStuHandle;
import Model.Student;
import Service.StudentService;

import java.util.Scanner;

public class StudentServicelmpl implements StudentService {
    @Override
    public void showStu(Student[] stus, int len) {
        DaoStuHandle daoStuHandle = new DaoStuHandle();
        daoStuHandle.showStuDAO(stus,len);
    }

    @Override
    public void addStu(Student[] stus, Student stu) {
        DaoStuHandle daoStuHandle = new DaoStuHandle();
        daoStuHandle.addStuDAO(stus,stu);
    }

    @Override
    public Boolean delStu(Student[] stus,Integer len, Integer Id)
    {
        int w=0;
        boolean flag=false;
        for (int i = 0; i < len; i++) {
            if (stus[i].getId()==Id){
                w=i;
                flag=true;
                break;
            }
        }
       if (flag){
           System.out.println("你确定要删除吗？y or n");
           Scanner scanner = new Scanner(System.in);
           char next = scanner.next().charAt(0);
           if (next=='y'){
               DaoStuHandle daoStuHandle = new DaoStuHandle();
               daoStuHandle.delStuDAO(stus,len,w);
               return true;
           }else {
               return null;
           }
       }
       return false;
    }
    public boolean updateStu(Student[] stus,int len,Integer Id){
        int w=0;
        boolean flag =false;
        for (int i = 0; i < len; i++) {
            if (stus[i].getId()==Id){
                w=i;
                flag=true;
                break;
            }
        }
        if (flag){
            DaoStuHandle daoStuHandle = new DaoStuHandle();
            daoStuHandle.updateStuDAO(stus,w);
        }
        return flag;
    }
}
